package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.AdvertService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Advert;
import com.iot08.common.response.Response;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @version 1.0
 * @Description: 媒体位管理
 * @author: Rock
 * @time: 2020/9/11 17:34
 */
@Controller
@RequestMapping("advert")
public class AdvertController extends BaseController {

    @Resource
    private AdvertService advertService;

    /**
     * @param [page, type, role, ip, startTime, endTime]
     * @time: 2020/9/11 18:07
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page, Integer type, Integer status) {
        ModelAndView mv = new ModelAndView();
        Page<Advert> pages = advertService.list(page, type, status, this.getSessionUser());
        mv.addObject("adverts", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("status", status);
        mv.addObject("type", type);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("advert/advert_list");
        return mv;
    }

    /**
     * 添加
     *
     * @time: 2020/10/22 14:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("advert/advert_add");
        return mv;
    }

    /**
     * @param advert
     * @param request
     * @time: 2020/10/22 18:30
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Advert advert, MultipartHttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
        advert.setCreator(u.getId());
        advertService.add(advert, request.getFile("imgUrlFile"),
                request.getFile("includeQRcodeImageFile"), request.getFile("bottomBtnImageFile"),
                this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param
     * @time: 2020/10/23 14:26
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAddOrEdit")
    public ModelAndView toAddOrEdit(Integer type) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("advert", advertService.getByType(type));
        mv.addObject("type", type);
        mv.setViewName("advert/advert_add_edit");
        return mv;
    }

    /**
     * @param advert
     * @param request
     * @time: 2020/10/22 18:30
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "addOrEdit", method = RequestMethod.POST)
    public Response<Object> addOrEdit(Advert advert, MultipartHttpServletRequest request) throws IOException {
        if (StringUtil.isBlank(advert.getId())) {
            //添加
            SysUser u = this.getSessionUser();
            advert.setCreator(u.getId());
            advertService.add(advert, request.getFile("imgUrlFile"),
                    request.getFile("includeQRcodeImageFile"), request.getFile("bottomBtnImageFile"),
                    this.getSessionUser(), this.getRemortIP());
        } else {
            //编辑
            advertService.update(advert, request.getFile("imgUrlFile"),
                    request.getFile("includeQRcodeImageFile"), request.getFile("bottomBtnImageFile"),
                    this.getSessionUser(), this.getRemortIP());
        }
        return Response.successResponse(advert.getId(), "保存成功： " + DateUtil.dateToStr(new Date(), DateUtil.FORMAT_1));
    }

    /**
     * 编辑页面
     *
     * @param id
     * @time: 2020/10/22 22:16
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("advert", advertService.findById(id));
        mv.setViewName("advert/advert_edit");
        return mv;
    }

    /**
     * @param advert
     * @param request
     * @time: 2020/10/22 22:42
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(Advert advert, MultipartHttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        advertService.update(advert, request.getFile("imgUrlFile"),
                request.getFile("includeQRcodeImageFile"), request.getFile("bottomBtnImageFile"),
                this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id
     * @param status
     * @time: 2020/10/22 22:44
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    public Response<Object> updateStatus(String id, Integer status) {
        return advertService.updateStatus(id, status, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * 删除
     *
     * @param [ids]
     * @time: 2020/9/14 9:41
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return advertService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }


}
